from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings


def generic_openid_token(openid):
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=60*60)
    data  = {'openid':openid}
    token  = serializer.dumps(data)
    return token.decode()

from itsdangerous import BadData
def check_save_user_token(access_token):
    serializer = Serializer(settings.SECRET_KEY, expires_in=60*60)
    try:
        data = serializer.loads(access_token)
    except BadData:
        return None
    openid = data.get('openid')
    return openid